﻿using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Security;
using System.Web.Profile;
using IronOne.SalesOne.BusinessObjects;
using System.Configuration;
using Facade;
using System.Collections;
using System.Data;

namespace IronOne.SalesOne.SalesOneWeb
{
    public partial class ManageBranches : SOSecurityPage, IDataPage, IntTest
    {
        public List<Branch> branchList;
        private int isChecked = 0;
        public List<Branch> branch;
        private int pageNo = 1;
        private int nodeId;




        protected void Page_Load(object sender, EventArgs e)
        {


            // grpID = Page.Request.Params["grpId"].ToString();

            //pageType = Page.Request.Params["group"].ToString();





            string postCause = this.Page.Request.Params.Get("__EVENTTARGET");

            if (string.IsNullOrEmpty(postCause))
            {
                postCause = "REDIRECT";
            }

            //lblHeader.Text = "Manage Branch";

            Session["usrType"] = "Manage Branch";
            btnManageUnit.Visible = false;
            btnManageUser.Visible = false;
            btnCreate.Visible = true;
            //lblResult.Text = "";

            //check this out
            UCGridButtons1.Visible = false;


            if (Session["sccBranch"] != null)
            {

                MassageSummery("IIS401");
                //lblResult.Text = Session["sccBranch"].ToString();
                //lblResult.ForeColor = System.Drawing.Color.Green;
                Session.Remove("sccBranch");
            }

            if (Session["failBranch"] != null)
            {
                MassageSummery("IIS502");
                //lblResult.Visible = true;
                //lblResult.Text = Session["failBranch"].ToString();
                //lblResult.ForeColor = System.Drawing.Color.Red;
                Session.Remove("failBranch");
            }

            GridView gr = (GridView)UCGrid1.FindControl("grdUsers");

            //foreach (GridViewRow row in gr.Rows)
            //{
            //    RadioButton rd = (RadioButton)row.Cells[0].FindControl("RadioButton1");
            //    if (rd.Checked == true)
            //    {
            //        isChecked = 1;
            //    }
            //}



            UCGridButtons1.SetCredentials(ValidateUserCredentials(Request.Url.LocalPath));

            DropDownList drpGroup = (DropDownList)UCSearch1.FindControl("drpdownGroup");


            if (!IsPostBack)
            {

                try
                {


                    if (Page.Request.Params["grpId"].ToString() != null)
                    {
                        StructureFacade fac = new StructureFacade();
                        //string g = Session["groupId"].ToString();



                        int gId = Convert.ToInt32(Page.Request.Params["grpId"].ToString());
                        nodeId = gId;
                        //UCGrid1.Visible = false;

                        drpGroup.SelectedValue = fac.GetGroup(gId).GroupId.ToString();

                        List<SalesOneGroup> gp = fac.GetGroups("GroupId ASC");
                        drpGroup.DataSource = gp;
                        SalesOneGroup sog = new SalesOneGroup();

                        //Branch brSel = new Branch();

                        sog.GroupId = -1;
                        sog.Name = "Select";
                        gp.Insert(0, sog);
                        drpGroup.DataTextField = "Name";
                        drpGroup.DataValueField = "GroupId";
                        drpGroup.DataBind();
                        drpGroup.SelectedValue = gId.ToString();



                        btnManageUser.Visible = true;
                        btnManageUnit.Visible = true;
                        UCGridButtons1.Visible = true;
                        btnCreate.Visible = false;


                        System.Web.UI.HtmlControls.HtmlInputHidden loadGrd = (System.Web.UI.HtmlControls.HtmlInputHidden)UCSearch1.FindControl("_SETGRIDSOURCE");


                        pageNo = 1;
                        UCGrid1.SetText("1");
                        branch = Source(gId, 1);

                        if (branch.Count == Convert.ToInt32(ConfigurationManager.AppSettings["GridCount"]))
                        {
                            UCGrid1.IsLastPage = (Source(gId, 2)).Count > 0 ? false : true;
                        }

                        if (postCause.Contains("lbtNext"))
                        {
                            if (UCGrid1.GetText() != "1")
                            {
                                pageNo = Convert.ToInt32(UCGrid1.GetText() + 1);
                                UCGrid1.SetText(pageNo.ToString());

                            }
                            else
                            {
                                pageNo = 2;
                                UCGrid1.SetText("2");

                            }

                            branch = Source(nodeId, pageNo);
                            if (branch.Count == Convert.ToInt32(ConfigurationManager.AppSettings["GridCount"]))
                            {
                                UCGrid1.IsLastPage = (Source(nodeId, pageNo + 1)).Count > 0 ? false : true;
                            }
                        }
                        else if (postCause.Contains("lbtPrev"))
                        {
                            if (UCGrid1.GetText() != "1")
                            {
                                pageNo = Convert.ToInt32(UCGrid1.GetText()) - 1;
                                UCGrid1.SetText(pageNo.ToString());

                                branch = Source(nodeId, pageNo);
                                if (branch.Count == Convert.ToInt32(ConfigurationManager.AppSettings["GridCount"]))
                                {
                                    UCGrid1.IsLastPage = (Source(nodeId, pageNo + 1)).Count > 0 ? false : true;
                                }
                            }
                        }
                        else if (postCause.Contains("lbtGo"))
                        {
                            int p = int.Parse(UCGrid1.GetText());
                            if (p != 0)
                            {
                                try
                                {
                                    branch = Source(nodeId, p);
                                    if (branch.Count == Convert.ToInt32(ConfigurationManager.AppSettings["GridCount"]))
                                    {
                                        UCGrid1.IsLastPage = (Source(nodeId, p + 1)).Count > 0 ? false : true;
                                    }
                                    pageNo = p;
                                }
                                catch (Exception)
                                {

                                    throw;
                                }


                            }

                        }



                        if (!postCause.Contains("RadioButton1"))
                        {
                            try
                            {

                                int[] numbers = { 1, 2, 3, 4, 6, 7, 8, 9, 10, 11 };



                                if (branch.Count != 0)
                                {

                                    //lblResult.Visible = false;
                                    UCGrid1.Visible = true;

                                    UCGrid1.styleList = new List<GridViewStyle> {
                new GridViewStyle{
                    ColumnId = "BranchName",
                    ColumnHeader = "Branch Name",
                    ColumnWidth = 130},
                new GridViewStyle{
                    ColumnId = "EffectiveFrom",
                    ColumnHeader = "Effective From",
                    ColumnWidth = 130},
                new GridViewStyle{
                    ColumnId = "EffectiveTo",
                    ColumnHeader = "Effective To",
                    ColumnWidth = 80}
                
            };




                                    UCGrid1.SetDataSource(branch, numbers);
                                    //Session.Remove("groupId");



                                }
                                else
                                {
                                    btnCreate.Visible = true;
                                    //lblResult.Visible = true;
                                    //lblResult.Text = "No Results to be displayed";
                                    //lblResult.ForeColor = System.Drawing.Color.Red;

                                    MassageSummery("IIS504");

                                    UCGridButtons1.Visible = false;
                                    UCGrid1.Visible = false;
                                    btnManageUnit.Visible = false;
                                    btnManageUser.Visible = false;

                                    //btnManageUsers.Visible = false;
                                    //UCGridButtons1.Visible = false;
                                }

                            }
                            catch (Exception ex)
                            {
                            }

                        }






                    }
                }
                catch (Exception ex)
                {
                }

            }


            #region commented (Implemented in latter part)










            //if (Session["groupId"] != null)
            //{
            //    StructureFacade fac = new StructureFacade();
            //    //string g = Session["groupId"].ToString();

            //    int gId = Convert.ToInt32(Session["groupId"].ToString());
            //    nodeId = gId;
            //    //UCGrid1.Visible = false;

            //    drpGroup.SelectedValue = fac.GetGroup(gId).GroupId.ToString();

            //    List<SalesOneGroup> gp = fac.GetGroups("GroupId ASC");
            //    drpGroup.DataSource = gp;
            //    SalesOneGroup sog = new SalesOneGroup();

            //    //Branch brSel = new Branch();

            //    sog.GroupId = -1;
            //    sog.Name = "Select";
            //    gp.Insert(0, sog);
            //    drpGroup.DataTextField = "Name";
            //    drpGroup.DataValueField = "GroupId";
            //    drpGroup.DataBind();
            //    drpGroup.SelectedValue = gId.ToString();



            //    btnManageUser.Visible = true;
            //    btnManageUnit.Visible = true;
            //    UCGridButtons1.Visible = true;
            //    btnCreate.Visible = false;


            //    System.Web.UI.HtmlControls.HtmlInputHidden loadGrd = (System.Web.UI.HtmlControls.HtmlInputHidden)UCSearch1.FindControl("_SETGRIDSOURCE");


            //        pageNo = 1;
            //        UCGrid1.SetText("1");
            //        branch = Source(gId, 1);

            //     if (postCause.Contains("lbtNext"))
            //    {
            //        if (UCGrid1.GetText() != "1")
            //        {
            //            pageNo = Convert.ToInt32(UCGrid1.GetText() + 1);
            //            UCGrid1.SetText(pageNo.ToString());

            //        }
            //        else
            //        {
            //            pageNo = 2;
            //            UCGrid1.SetText("2");

            //        }

            //        branch = Source(nodeId, pageNo);

            //    }
            //    else if (postCause.Contains("lbtPrev"))
            //    {
            //        if (UCGrid1.GetText() != "1")
            //        {
            //            pageNo = Convert.ToInt32(UCGrid1.GetText()) - 1;
            //            UCGrid1.SetText(pageNo.ToString());

            //            branch = Source(nodeId, pageNo);
            //        }
            //    }
            //    else if (postCause.Contains("lbtGo"))
            //    {
            //        int p = int.Parse(UCGrid1.GetText());
            //        if (p != 0)
            //        {
            //            try
            //            {
            //                branch = Source(nodeId, p);
            //                pageNo = p;
            //            }
            //            catch (Exception)
            //            {

            //                throw;
            //            }


            //        }

            //    }



            //     if (!postCause.Contains("RadioButton1"))
            //     {
            //         try{

            //             int[] numbers = { 1, 3, 6, 7, 8, 9, 10, 11 };



            //             if (branch.Count != 0)
            //             {

            //                 //lblResult.Visible = false;
            //                 UCGrid1.Visible = true;
            //                 UCGrid1.SetDataSource(branch, numbers);
            //                 Session.Remove("groupId");



            //             }
            //             else
            //             {
            //                 btnCreate.Visible = true;
            //                 //lblResult.Visible = true;
            //                 //lblResult.Text = "No Results to be displayed";
            //                 //lblResult.ForeColor = System.Drawing.Color.Red;

            //                 MassageSummery("IIS504");

            //                 UCGridButtons1.Visible = false;
            //                 UCGrid1.Visible = false;
            //                 btnManageUnit.Visible = false;
            //                 btnManageUser.Visible = false;

            //                 //btnManageUsers.Visible = false;
            //                 //UCGridButtons1.Visible = false;
            //             }

            //           }
            //        catch (Exception ex)
            //        {
            //        }

            //         }






            //}

            #endregion



            else if (drpGroup != null && drpGroup.SelectedValue != "-1")
            {

                btnManageUser.Visible = true;
                btnManageUnit.Visible = true;
                UCGridButtons1.Visible = true;
                btnCreate.Visible = false;

                int grpId = Convert.ToInt32(drpGroup.SelectedValue);

                nodeId = grpId;


                System.Web.UI.HtmlControls.HtmlInputHidden loadGrd = (System.Web.UI.HtmlControls.HtmlInputHidden)UCSearch1.FindControl("_SETGRIDSOURCE");
                //loadGrd.Value = "1";

                // List<Branch> branch = Source(grpId);

                if (postCause.Contains("drpdownGroup"))
                {
                    pageNo = 1;
                    UCGrid1.SetText("1");
                    branch = Source(grpId, 1);
                    if (branch.Count == Convert.ToInt32(ConfigurationManager.AppSettings["GridCount"]))
                    {
                        UCGrid1.IsLastPage = (Source(grpId, 2)).Count > 0 ? false : true;
                    }
                }
                else if (postCause.Contains("lbtNext"))
                {
                    if (UCGrid1.GetText() != "1")
                    {
                        pageNo = Convert.ToInt32(UCGrid1.GetText() + 1);
                        UCGrid1.SetText(pageNo.ToString());

                    }
                    else
                    {
                        pageNo = 2;
                        UCGrid1.SetText("2");

                    }

                    branch = Source(nodeId, pageNo);
                    if (branch.Count == Convert.ToInt32(ConfigurationManager.AppSettings["GridCount"]))
                    {
                        UCGrid1.IsLastPage = (Source(nodeId, pageNo + 1)).Count > 0 ? false : true;
                    }
                }
                else if (postCause.Contains("lbtPrev"))
                {
                    if (UCGrid1.GetText() != "1")
                    {
                        pageNo = Convert.ToInt32(UCGrid1.GetText()) - 1;
                        UCGrid1.SetText(pageNo.ToString());

                        branch = Source(nodeId, pageNo);
                        if (branch.Count == Convert.ToInt32(ConfigurationManager.AppSettings["GridCount"]))
                        {
                            UCGrid1.IsLastPage = (Source(nodeId, pageNo + 1)).Count > 0 ? false : true;
                        }
                    }
                }
                else if (postCause.Contains("lbtGo"))
                {
                    int p = int.Parse(UCGrid1.GetText());
                    if (p != 0)
                    {
                        try
                        {
                            branch = Source(nodeId, p);
                            if (branch.Count == Convert.ToInt32(ConfigurationManager.AppSettings["GridCount"]))
                            {
                                UCGrid1.IsLastPage = (Source(nodeId, p + 1)).Count > 0 ? false : true;
                            }
                            pageNo = p;
                        }
                        catch (Exception)
                        {

                            throw;
                        }


                    }

                }



                if (!postCause.Contains("RadioButton1"))
                {
                    try
                    {
                        int[] numbers = { 1, 2, 3, 4, 6, 7, 8, 9, 10, 11 };
                        if (branch.Count != 0)
                        {
                            //lblResult.Visible = false;
                            UCGrid1.Visible = true;

                            UCGrid1.styleList = new List<GridViewStyle> {
                new GridViewStyle{
                    ColumnId = "BranchName",
                    ColumnHeader = "Branch Name",
                    ColumnWidth = 130},
                new GridViewStyle{
                    ColumnId = "EffectiveFrom",
                    ColumnHeader = "Effective From",
                    ColumnWidth = 130},
                new GridViewStyle{
                    ColumnId = "EffectiveTo",
                    ColumnHeader = "Effective To",
                    ColumnWidth = 80}
                
            };




                            UCGrid1.SetDataSource(branch, numbers);


                        }
                        else
                        {
                            btnCreate.Visible = true;
                            //lblResult.Visible = true;
                            //lblResult.Text = "No Results to be displayed";
                            //lblResult.ForeColor = System.Drawing.Color.Red;

                            MassageSummery("IIS504");

                            UCGridButtons1.Visible = false;
                            UCGrid1.Visible = false;
                            btnManageUnit.Visible = false;
                            btnManageUser.Visible = false;
                            //btnManageUsers.Visible = false;
                            //UCGridButtons1.Visible = false;
                        }
                    }
                    catch (Exception ex) { }
                }

            }
            else
            {
                UCGridButtons1.Visible = false;
                //UCGrid1.SetDataSource(branch, numbers);
            }
        }


        public List<Branch> DataSource()
        {
            DataTable brTable = new DataTable();
            Facade.StructureFacade bf = new Facade.StructureFacade();

            branchList = bf.GetBranches("BranchId ASC");
            return branchList;

        }





        private List<Branch> Source(int nodeId, int pageNo)
        {
            StructureFacade fac = new StructureFacade();
            return fac.GetBranchesForGroup(nodeId,pageNo);

        }


        #region IDataPage Members

        public void Create()
        {


            GridView gr = (GridView)UCGrid1.FindControl("grdUsers");

            foreach (GridViewRow row in gr.Rows)
            {
                RadioButton rbtn = (RadioButton)row.FindControl("RadioButton1");
                if (rbtn.Checked)
                {
                    Response.Redirect("~/Structure/AddBranch.aspx?brnchId_c=" + row.Cells[2].Text);
                }
                else
                {
                    MassageSummery("IIS503");
                }
            }        
            
            
           // Response.Redirect("~/Structure/AddBranch.aspx");
        }

        public void View()
        {
            // Response.Redirect("~/Structure/AddBranch.aspx?a=v");

            //if (Session["branchId"] == null)
            //{

            //    Session["branchId"] = ((GridViewRow)Session["firstRow"]).Cells[2].Text;
            //    Session.Remove("firstRow");
            //}

            GridView gr = (GridView)UCGrid1.FindControl("grdUsers");

            foreach (GridViewRow row in gr.Rows)
            {
                RadioButton rbtn = (RadioButton)row.FindControl("RadioButton1");
                if (rbtn.Checked)
                {
                    Response.Redirect("~/Structure/AddBranch.aspx?a=v&Bid=" + row.Cells[2].Text);
                }
                else
                {
                    MassageSummery("IIS503");
                }
            }


            

            //if (Session["branchId"] != null)
            //{
            //    Response.Redirect("~/Structure/AddBranch.aspx?a=v");
            //}
            //else
            //{
            //    //lblResult.Visible = true;
            //    //lblResult.Text = "Please Select a branch to be viewed";
            //    //lblResult.ForeColor = System.Drawing.Color.Red;

            //    MassageSummery("IIS503");

            //}

        }

        public void Update()
        {
            //Response.Redirect("~/Structure/AddBranch.aspx?a=u");

            GridView gr = (GridView)UCGrid1.FindControl("grdUsers");

            foreach (GridViewRow row in gr.Rows)
            {
                RadioButton rbtn = (RadioButton)row.FindControl("RadioButton1");
                if (rbtn.Checked)
                {
                    Response.Redirect("~/Structure/AddBranch.aspx?a=u&Bid_u=" + row.Cells[2].Text);
                }
                else
                {
                    MassageSummery("IIS503");
                }
            }


            //if (Session["branchId"] == null)
            //{

            //    Session["branchId"] = ((GridViewRow)Session["firstRow"]).Cells[2].Text;
            //    Session.Remove("firstRow");
            //}

            //if (Session["branchId"] != null)
            //{
            //    Response.Redirect("~/Structure/AddBranch.aspx?a=u");
            //}
            //else
            //{
            //    //lblResult.Visible = true;
            //    //lblResult.Text = "Please Select a branch to be updated";
            //    //lblResult.ForeColor = System.Drawing.Color.Red;

            //    MassageSummery("IIS503");
            
            //}


        }

        public void Delete()
        {
            StructureFacade fac = new StructureFacade();
            string branch = Session["branchId"].ToString();
            Branch br = fac.GetBranch(Convert.ToInt32(branch));

            fac.DeleteBranch(br);
        }

        public void Cancel()
        {
            Response.Redirect("~/Structure/ManageBranches.aspx");
        }




        #endregion



        public void GetId(GridViewRow row)
        {
            Session["branchId"] = row.Cells[2].Text;
            //lblResult.Visible = false;
        }

        protected void btnManageUser_Click(object sender, EventArgs e)
        {
            #region commented
            //Response.Redirect("~/UserManagement/ManageUsers.aspx?x=b");

            //if (Session["branchId"] == null)
            //{

            //    Session["branchId"] = ((GridViewRow)Session["firstRow"]).Cells[2].Text;
            //    Session.Remove("firstRow");
            //}

            //if (Session["branchId"] != null)
            //{
            //    Response.Redirect("~/UserManagement/ManageUsers.aspx?x=b");
            //}
            //else
            //{
            //    //lblResult.Visible = true;
            //    //lblResult.Text = "Please Select a branch";
            //    //lblResult.ForeColor = System.Drawing.Color.Red;

            //    MassageSummery("IIS503");

            //}
            
            #endregion


            GridView gr = (GridView)UCGrid1.FindControl("grdUsers");

            foreach (GridViewRow row in gr.Rows)
            {
                RadioButton rbtn = (RadioButton)row.FindControl("RadioButton1");
                if (rbtn.Checked)
                {
                    Response.Redirect("~/UserManagement/ManageUsers.aspx?x=b&bId_m_b="  +row.Cells[2].Text);
                }
                else
                {
                    MassageSummery("IIS503");
                }
            }


        }

        protected void btnManageUnit_Click(object sender, EventArgs e)
        {
            #region commented
            //Response.Redirect("~/Structure/ManageUnits.aspx");

            //if (Session["branchId"] == null)
            //{

            //    Session["branchId"] = ((GridViewRow)Session["firstRow"]).Cells[2].Text;
            //    Session.Remove("firstRow");
            //}

            //if (Session["branchId"] != null)
            //{
            //    Response.Redirect("~/Structure/ManageUnits.aspx");
            //}
            //else
            //{
            //    //lblResult.Visible = true;
            //    //lblResult.Text = "Please Select a branch";
            //    //lblResult.ForeColor = System.Drawing.Color.Red;

            //    MassageSummery("IIS503");

            //} 
            #endregion

            GridView gr = (GridView)UCGrid1.FindControl("grdUsers");

            foreach (GridViewRow row in gr.Rows)
            {
                RadioButton rbtn = (RadioButton)row.FindControl("RadioButton1");
                if (rbtn.Checked)
                {
                    Response.Redirect("~/Structure/ManageUnits.aspx?brnchId=" + row.Cells[2].Text);
                }
                else
                {
                    MassageSummery("IIS503");
                }
            }








        }

        protected void btnCreate_Click(object sender, EventArgs e)
        {
            DropDownList drpGroup = (DropDownList)UCSearch1.FindControl("drpdownGroup");
            string grp = drpGroup.SelectedValue.ToString();

            Response.Redirect("~/Structure/AddBranch.aspx?grpId_c=" + grp);
        }





        #region IntTest Members

        public void Next()
        {
            //if (UCGrid1.GetText() != "1")
            //{
            //    pageNo = Convert.ToInt32(UCGrid1.GetText()) + 1;
            //    UCGrid1.SetText(pageNo.ToString());

            //}
            //else
            //{
            //    pageNo = 2;
            //    UCGrid1.SetText("2");

            //}

            //Source(nodeId, pageNo);
        }

        public void Previous()
        {
            //if (UCGrid1.GetText() != "1")
            //{
            //    pageNo = Convert.ToInt32(UCGrid1.GetText()) - 1;
            //    UCGrid1.SetText(pageNo.ToString());

            //    Source(nodeId, pageNo);
            //}
        }

        public void Go()
        {
            //int p = int.Parse(UCGrid1.GetText());
            //if (p != 0)
            //{
            //    try
            //    {
            //        Source(nodeId, p);
            //        pageNo = p;
            //    }
            //    catch (Exception)
            //    {

            //        throw;
            //    }


            //}
        }

        #endregion
    }
}

